<?php

/**
 * default actions.
 *
 * @package    www.sflast.com
 * @subpackage default
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class defaultActions extends sfActions
{
  /**
   * Executes index action
   *
   */


  public function executeIndex()
  {
    //yesだったらリセットする処理を作る。
    //indexSuccessで何かが送信されてきたら、それに反応させるように。getRequestParameterで。
    $reset_all = $this->getRequestParameter("reset_all");
    if($reset_all === "y"){
        $this->setflash("search_place", "");
        $this->setflash("hinichi", "");
        $this->setflash("hidmaybetype", "");
        $this->setflash("hidmaybecolor", "");
        $this->setflash("maybetype", NULL);
        $this->setflash("maybecolor", NULL);
    }

    //$this->search_place = $this->getRequestParameter('search_place');
    //$search_place = $this->search_place;
    //$this->getUser()->setFlash("search_place", $search_place);

    ///////////////セッション問題//////////////////////////////
    $area = $this->getRequestParameter("search_place");
    //リクエストパラメータがあれば
    if($area){
        $this->setflash("search_place", $area);
    //なければ
    }else{
        $area = $this->getflash("search_place");
        $this->setflash("search_place", $area);
    }
    $this->search_place = $area;

    ////////////////////////////////////////////////////////
    $area = $this->search_place;


    $found = $this->getRequestParameter("found");
    if($found){
        $this->setflash("found", $found);
    }else{
        $found = $this->getflash("found");
        $this->setflash("found", $found);
    }
    $this->found = $found;


    $hidmaybetype = $this->getRequestParameter("hidmaybetype");
    //echo $hidmaybetype;
    if($hidmaybetype){
        $this->setflash("hidmaybetype", $hidmaybetype);
    }else{
        $hidmaybetype = $this->getflash("hidmaybetype");
        $this->setflash("hidmaybetype", $hidmaybetype);
    }
    $this->hidmaybetype = $hidmaybetype;


    $hidmaybecolor = $this->getRequestParameter("hidmaybecolor");
    if($hidmaybecolor){
        $this->setflash("hidmaybecolor", $hidmaybecolor);
    }else{
        $hidmaybecolor = $this->getflash("hidmaybecolor");
        $this->setflash("hidmaybecolor", $hidmaybecolor);
    }
    $this->hidmaybecolor = $hidmaybecolor;


    $this->year = sfConfig::get("sf_year");
    $this->month = sfConfig::get("sf_month");
    $this->days = sfConfig::get("sf_days");


    $this->search_arr = array(

      "module" => "default",
      "action" => "index",

      //"order_by" => $this->getRequestParameter("order_by", "asc"),

      "hidmaybetype" => $this->getRequestParameter('hidmaybetype'),
      "hidmaybecolor" => $this->getRequestParameter('hidmaybecolor'),
      //$this->search_place = $this->getRequestParameter('area'),
      //"area" => $this->getRequestParameter('area'),
      //areaはgooglemapsで普通に場所検索できればいいなー

      "year" => $this->getRequestParameter('year'),
      "month" => $this->getRequestParameter('month'),
      "days" => $this->getRequestParameter('days')//,

     // "category" => $this->getRequestParameter('category')

    );

        ////上の検索からさらに詳細検索したい場合。（日付）

    $hinichi = $this->getRequestParameter('year')
    ."-".sprintf("%02d", $this->getRequestParameter('month'))
    ."-".sprintf("%02d", $this->getRequestParameter('days'));
    //"-".sprintf("%02d", ～)はdate関数に合わせてyyyy-mm-ddという形に直すため。
    ///echo $hinichi;
     //$area = $this->getRequestParameter("search_place");
    //リクエストパラメータがあれば
    if($hinichi){
        $this->setflash("hinichi", $hinichi);
    //なければ
    }else{
        $hinichi = $this->getflash("hinichi");
        $this->setflash("hinichi", $hinichi);
    }
    $this->hinichi = $hinichi;


// シンプルな日にち検索。

    $c = new Criteria();
    //ここで条件分岐する。
    if($hinichi != "-00-00"){
    //echo "まだ検索していません。";
    //}else{
        $c->add(FounditPeer::HINICHI, $hinichi, Criteria::GREATER_EQUAL);
        $found = FounditPeer::doSelect($c);
        $this->setflash("found", $found);
        $this->found = $found;
    }


    //まずはタイプで合致したものの名前とlatlngの情報を引き出すこと。
    //$maybetype = $this->getRequestParameter('hidmaybetype');
    //echo $hidmaybetype;exit;

    if($hidmaybetype != NULL){
    $a = new Criteria();
    //$a->add(FounditPeer::TYPE, $hidmaybetype);
    $a->add(FounditPeer::TYPE, '%'.$hidmaybetype.'%', Criteria::LIKE);
    $maybetype = FounditPeer::doSelect($a);
    if($maybetype){
        $this->setflash("maybetype", $maybetype);
    }else{
        $maybetype = $this->getflash("maybetype");
        $this->setflash("maybetype", $maybetype);
    }
    $this->maybetype = $maybetype;
    }

    //絞込み検索の場合。
    //$maybecolor = $this->getRequestParameter('hidmaybecolor');
    if($hidmaybetype != NULL){
    $e = new Criteria();
    $e->add(FounditPeer::TYPE, '%'.$hidmaybetype.'%', Criteria::LIKE);
    //$e->add(FounditPeer::TYPE, $hidmaybetype, Criteria::EQUAL);
    (isset($hidmaybecolor))?$e->add(FounditPeer::COLOR, '%'.$hidmaybecolor.'%', Criteria::LIKE):"";
    //(isset($hidmaybecolor))?$e->add(FounditPeer::COLOR, $hidmaybecolor, Criteria::EQUAL):"";
    //ifのやり方でできるか確認すべし。
    if($hinichi != "-00-00"){
        $e->add(FounditPeer::HINICHI, $hinichi, Criteria::GREATER_EQUAL);
    }
    //(isset($found))?$e->add(FounditPeer::HINICHI, $found, Criteria::GREATER_EQUAL):"";
    $maybecolor = FounditPeer::doSelect($e);
    //echo $color."<br>";
    //echo $hidmaybetype;

    if($maybecolor){
        $this->setflash("maybecolor", $maybecolor);
    }else{
        $maybecolor = $this->getflash("maybecolor");
        $this->setflash("maybecolor", $maybecolor);
    }
    $this->maybecolor = $maybecolor;
    //$area = $this->getRequestParameter('area');
    //$this->search_place = $this->getRequestParameter('area');
    }

    //$this->search_arr["order_by"] = "desc";

/*    $b = new Criteria();
    $b->add(FounditPeer::TYPE, $type);
    $b->addAnd(FounditPeer::COLOR, $color);
    $b->addAnd(FounditPeer::AREA, $area);
    //$b = FounditPeer::setOrderByParam($search_arr["order_by"], $b);
    $rst = FounditPeer::doSelect($b);

    $this->rst = $rst;
*/    //見つかったものをＩＤごとに全部引っ張ってくる。

$this->item_categorys = sfConfig::get("sf_item_categorys");
$category = $this->getRequestParameter('category');
//$category = $this->item_categorys;
print_r($category);



//今登録されている件数を数えたい。
$this->cnt = FounditPeer::getNumberFoundit();
//Peerファイルに移した。
//        $d = new Criteria();
//        $d->add(FounditPeer::ID, 1, Criteria::GREATER_EQUAL);
//        $count = FounditPeer::doSelect($d);
//        $cnt = count($count);
//        $this->cnt = $cnt;



//var_dump($this->search_place);
//var_dump($this->maybetype);
//var_dump($this->maybecolor);
//exit;
    //echo "hoge";
    return sfView::SUCCESS;
    //$this->forward('default', 'module');
  }


  public function executeError404()
  {
    $this->redirect("/");
  }



}
